﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Common.Query;
using BEGDC.Utility.Castle;
using BEGDC.CC.Entity;

namespace BEGDC.CC.Web.Common
{
    /// <summary>
    /// Summary description for CommonQuery.
    /// </summary>
    public class CommonQuery : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.TextBox tbComment;
        protected System.Web.UI.WebControls.Button Btn_Confirm;
        protected System.Web.UI.WebControls.Button Btn_Close;
        protected System.Web.UI.WebControls.RadioButtonList rblStep;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {
                string type = Request.QueryString["FlowType"];
                string step = Request.QueryString["FlowStep"];
                string appId = Request.QueryString["AppID"];
                BindSteps(type, step, appId);
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Btn_Confirm.Click += new EventHandler(Btn_Confirm_Click);
            this.Btn_Close.Click += new EventHandler(Btn_Close_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void BindSteps(string type, string step, string appId)
        {
            IQuery queryService = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.QueryService] as IQuery;
            Application app = queryService.GetApplication(Convert.ToInt32(appId));

            ProcessState states = (BEGDC.CC.BL.Workflow.Configuration.GetProcessStateList(type))[0];
            for (int i = 0; i < states.ChildrenStates.Count; i++)
            {
                string temp = states.ChildrenStates[i].Key;
                if (temp == step)
                    break;
                if (type == WorkflowTypes.CV)
                {
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.CAT && temp == WorkTransition.CV_PSOApprove)
                        continue;
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.Branch && temp == WorkTransition.CV_CATDIRECTORApprove)
                        continue;
                }
                if (type == WorkflowTypes.EV)
                {
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.CAT && (temp == WorkTransition.EV_PSOApprove || temp == WorkTransition.EV_PSOApproveMM))
                        continue;
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.Branch && (temp == WorkTransition.EV_CATDirectorApprove || temp == WorkTransition.EV_CATApproveMM))
                        continue;
                }
                if (type == WorkflowTypes.CR)
                {
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.CAT && (temp == WorkTransition.CR_FNCApproval
                        || temp == WorkTransition.CR_PSOHeadApproval
                        || temp == WorkTransition.CR_DeptDirectorApproval
                        || temp == WorkTransition.CR_DeptHeadApproval))
                        continue;
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.Branch && (temp == WorkTransition.CR_FNCApproval
                        || temp == WorkTransition.CR_CATDirectorApproval
                        || temp == WorkTransition.CR_CATVPApproval
                        || temp == WorkTransition.CR_DeptDirectorApproval
                        || temp == WorkTransition.CR_DeptHeadApproval))
                        continue;
                    if (app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.OtherDepartment && (temp == WorkTransition.CR_PSOHeadApproval
                        || temp == WorkTransition.CR_FNCApproval
                        || temp == WorkTransition.CR_CATDirectorApproval
                        || temp == WorkTransition.CR_CATVPApproval))
                        continue;
                    if (!app.IsTransition && (temp == WorkTransition.CR_CRSurvey))
                    {
                        continue;
                    }
                    if (!app.IsTransition && (temp == WorkTransition.CR_CATVPApproval))
                    {
                        continue;
                    }
                }
                if (type == WorkflowTypes.CO)
                {
                    //					if(app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.CAT && ( temp == WorkTransition.EV_PSOApprove || temp == WorkTransition.EV_PSOApproveMM ))
                    //						continue;
                    //					if(app.DepartmentType.DicItemKey == SysDicItem.DepartmentType.Branch && ( temp == WorkTransition.EV_CATDirectorApprove || temp == WorkTransition.EV_CATApproveMM ))
                    //						continue;
                }
                if (type == WorkflowTypes.OCR)
                {
                    if (app.IsSSME && (temp == WorkTransition.OCR_DeptHeadApproval || temp == WorkTransition.OCR_FNCApproval))
                    {
                        continue;
                    }
                    if (!app.IsSSME && (temp == WorkTransition.OCR_CostCenterHeadApproval || temp == WorkTransition.OCR_SSMEHeadApproval))
                    {
                        continue;
                    }
                }
                if (temp == WorkTransition.CR_ReRequest || temp == WorkTransition.CO_ReRequest || temp == WorkTransition.OCR_ReRequest)
                    continue;

                string actor = queryService.GetActorByAppStep(app, temp);
                if (!string.IsNullOrEmpty(actor))
                {
                    //temp = temp + "(" + actor + ")";
                    ListItem item = new ListItem(temp + "(" + actor + ")", temp);
                    rblStep.Items.Add(item);
                }
            }
            //insert ReportToApprove
            DataTable table = queryService.GetApplicationActionLogByActionName(app.Id, BEGDC.CC.BL.Workflow.Action.ReportToCheck);
            if (table != null && table.Rows.Count > 0 && (type == WorkflowTypes.OCR || type == WorkflowTypes.OCV) && app.RequesterNo.ReportTo != null && app.IsSSME)
            {
                string temp = BEGDC.CC.BL.Workflow.Action.ReportToCheck;
                string actor = app.RequesterNo.ReportTo.NameEN;
                string activeStep = (type == WorkflowTypes.OCR) ? BEGDC.CC.BL.Workflow.WorkTransition.OCR_ReportTo: BEGDC.CC.BL.Workflow.WorkTransition.OCV_ReportTo;
                ListItem item = new ListItem(temp + "(" + actor + ")", activeStep);
                rblStep.Items.Insert(1, item);
            }
            this.rblStep.Items[0].Selected = true;
        }

        void Btn_Confirm_Click(object sender, EventArgs e)
        {
            if (this.rblStep.SelectedItem != null)
            {
                Session["Comment"] = tbComment.Text;
                Response.Write(@"<Script>returnValue='" + this.rblStep.SelectedValue + "';self.close(); </Script>");
            }
            else
            {
                Page.RegisterClientScriptBlock("", "<script>alert('You must select one step to query!');window.close();</script>");
                return;
            }
        }

        void Btn_Close_Click(object sender, EventArgs e)
        {
            Response.Write(@"<Script>returnValue='%close%';self.close(); </Script>");
        }
    }
}
